package com.ayf.payment.game.api.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.ayf.payment.game.api.dto.ayb.*;
import com.ayf.payment.game.api.dto.ayf.MerchantTwoAyfDTO;
import com.ayf.payment.game.api.dto.criteria.SignPayProductCriteria;
import com.ayf.payment.game.api.entity.SignPayProduct;
import com.ayf.payment.game.api.mapper.SignPayProductMapper;
import com.ayf.payment.game.api.service.SignPayProductService;
import com.ayf.payment.game.api.service.platform.PaymentProductService;
import com.ayf.payment.game.api.utils.JsonUtils;
import com.ayf.payment.game.api.utils.SensitiveDataUtil;
import com.swwx.charm.commons.lang.utils.BeanConvertUtils;
import com.swwx.charm.commons.lang.utils.LogPortal;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class SignPayProductServiceImpl implements SignPayProductService {

    @Resource
    private SignPayProductMapper signPayProductMapper;

    @Resource
    private PaymentProductService paymentProductService;

    @Override
    public int insertSignPayProduct(SignPayProductDTO dto) {
        SignPayProduct signPayProduct=new SignPayProduct();
        signPayProduct.setApplyStatus(dto.getApplyStatus());
        Date date=new Date();
        signPayProduct.setCreateTime(date);
        signPayProduct.setChannelMerchantId(dto.getChannelMerchantId());
        signPayProduct.setMerchantId(dto.getMerchantId());
        signPayProduct.setLastUpdateTime(date);
        signPayProduct.setApplyStatus(dto.getApplyStatus());
        signPayProduct.setPayCode(dto.getPayCode());
        MerchantTwo2DTO merchantTwo2DTO=new MerchantTwo2DTO();
        merchantTwo2DTO.setSignedName(dto.getMerchantTwoDTO().getSignedName());
        merchantTwo2DTO.setEmail(dto.getMerchantTwoDTO().getEmail());
        merchantTwo2DTO.setPhone(dto.getMerchantTwoDTO().getPhone());
        merchantTwo2DTO.setProfession(dto.getMerchantTwoDTO().getProfession());
        merchantTwo2DTO.setZzLegalPersonIdNo(dto.getMerchantTwoDTO().getZzLegalPersonIdNo());
        merchantTwo2DTO.setAccountName(dto.getMerchantTwoDTO().getAccountName());
        merchantTwo2DTO.setBankCardNo(dto.getMerchantTwoDTO().getBankCardNo());
        merchantTwo2DTO.setBusinessAddress(dto.getMerchantTwoDTO().getBusinessAddress());
        merchantTwo2DTO.setBusinessAddressArea(dto.getMerchantTwoDTO().getBusinessAddressArea());
        merchantTwo2DTO.setBusinessAddressCity(dto.getMerchantTwoDTO().getBusinessAddressCity());
        merchantTwo2DTO.setBusinessAddressProvince(dto.getMerchantTwoDTO().getBusinessAddressProvince());
        merchantTwo2DTO.setChannelMerchantId(dto.getMerchantTwoDTO().getChannelMerchantId());
        merchantTwo2DTO.setProvinceCode(dto.getMerchantTwoDTO().getProvinceCode());
        merchantTwo2DTO.setCityCode(dto.getMerchantTwoDTO().getCityCode());
        merchantTwo2DTO.setEmail(dto.getMerchantTwoDTO().getEmail());
        merchantTwo2DTO.setLegalIdCardConsPath(dto.getMerchantTwoDTO().getLegalIdCardConsPath());
        merchantTwo2DTO.setLegalPersonBankCardPath(dto.getMerchantTwoDTO().getLegalPersonBankCardPath());
        merchantTwo2DTO.setLegalIdCardProsPath(dto.getMerchantTwoDTO().getLegalIdCardProsPath());
        String applyInfo= JsonUtils.toJson(merchantTwo2DTO);
        signPayProduct.setApplyInfo(applyInfo);
        signPayProductMapper.insertSelective(signPayProduct);
        return signPayProduct.getId();
    }

    @Override
    public int insertAyfSignPayProduct(SignPayProductDTO dto) {
        SignPayProduct signPayProduct=new SignPayProduct();
        signPayProduct.setApplyStatus(dto.getApplyStatus());
        Date date=new Date();
        signPayProduct.setCreateTime(date);
        signPayProduct.setChannelMerchantId(dto.getChannelMerchantId());
        signPayProduct.setMerchantId(dto.getMerchantId());
        signPayProduct.setLastUpdateTime(date);
        signPayProduct.setApplyStatus(dto.getApplyStatus());
        signPayProduct.setPayCode(dto.getPayCode());
        String applyInfo= JsonUtils.toJson(dto.getMerchantTwoAyfDTO());
        signPayProduct.setApplyInfo(applyInfo);
        signPayProductMapper.insertSelective(signPayProduct);
        return signPayProduct.getId();
    }

    @Override
    public Integer insertSignPayProduct2(SignPayProductDTO dto) {
        SignPayProduct signPayProduct=new SignPayProduct();
        signPayProduct.setApplyStatus(dto.getApplyStatus());
        Date date=new Date();
        signPayProduct.setCreateTime(date);
        signPayProduct.setChannelMerchantId(dto.getChannelMerchantId());
        signPayProduct.setMerchantId(dto.getMerchantId());
        signPayProduct.setLastUpdateTime(date);
        signPayProduct.setApplyStatus(dto.getApplyStatus());
        signPayProduct.setPayCode(dto.getPayCode());
        signPayProduct.setApplyInfo(dto.getApplyInfo());
        signPayProductMapper.insertSelective(signPayProduct);
        return signPayProduct.getId();
    }


    @Override
    public void updateSignPayProduct(SignPayProductDTO dto) {
        SignPayProduct signPayProduct=signPayProductMapper.selectByPrimaryKey(dto.getId());
            signPayProduct.setId(dto.getId());
            signPayProduct.setFailMsg(dto.getFailMsg());
            signPayProduct.setApplyStatus(dto.getApplyStatus());
            signPayProduct.setUpdateStatus(dto.getUpdateStatus());
            signPayProduct.setMerchantId(dto.getMerchantId());
            signPayProduct.setChannelMerchantId(dto.getChannelMerchantId());
            signPayProduct.setPayCode(dto.getPayCode());
            if(dto.getUpdateStatus()>0) {
                MerchantTwo2DTO merchantTwo2DTO = new MerchantTwo2DTO();
                merchantTwo2DTO.setSignedName(dto.getMerchantTwoDTO().getSignedName());
                merchantTwo2DTO.setEmail(dto.getMerchantTwoDTO().getEmail());
                merchantTwo2DTO.setPhone(dto.getMerchantTwoDTO().getPhone());
                merchantTwo2DTO.setProfession(dto.getMerchantTwoDTO().getProfession());
                merchantTwo2DTO.setZzLegalPersonIdNo(dto.getMerchantTwoDTO().getZzLegalPersonIdNo());
                merchantTwo2DTO.setAccountName(dto.getMerchantTwoDTO().getAccountName());
                merchantTwo2DTO.setBankCardNo(dto.getMerchantTwoDTO().getBankCardNo());
                merchantTwo2DTO.setBusinessAddress(dto.getMerchantTwoDTO().getBusinessAddress());
                merchantTwo2DTO.setBusinessAddressArea(dto.getMerchantTwoDTO().getBusinessAddressArea());
                merchantTwo2DTO.setBusinessAddressCity(dto.getMerchantTwoDTO().getBusinessAddressCity());
                merchantTwo2DTO.setBusinessAddressProvince(dto.getMerchantTwoDTO().getBusinessAddressProvince());
                merchantTwo2DTO.setChannelMerchantId(dto.getMerchantTwoDTO().getChannelMerchantId());
                merchantTwo2DTO.setProvinceCode(dto.getMerchantTwoDTO().getProvinceCode());
                merchantTwo2DTO.setCityCode(dto.getMerchantTwoDTO().getCityCode());
                merchantTwo2DTO.setEmail(dto.getMerchantTwoDTO().getEmail());
                merchantTwo2DTO.setLegalIdCardConsPath(dto.getMerchantTwoDTO().getLegalIdCardConsPath());
                merchantTwo2DTO.setLegalPersonBankCardPath(dto.getMerchantTwoDTO().getLegalPersonBankCardPath());
                merchantTwo2DTO.setLegalIdCardProsPath(dto.getMerchantTwoDTO().getLegalIdCardProsPath());
                String applyInfo = JsonUtils.toJson(merchantTwo2DTO);
                signPayProduct.setApplyInfo(applyInfo);
            }
            signPayProduct.setLastUpdateTime(new Date());
            if (dto.getApplyStatus() != null && dto.getApplyStatus() == 2) {
                signPayProduct.setFinishTime(new Date());
            }
            signPayProductMapper.updateByPrimaryKeySelective(signPayProduct);

    }

    @Override
    public void updateAyfSignPayProductById(SignPayProductDTO dto) {
        SignPayProduct signPayProduct = BeanConvertUtils.convert(dto, SignPayProduct.class);
        if (dto.getApplyStatus() != null && dto.getApplyStatus() == 2) {
            signPayProduct.setFinishTime(new Date());
        }
        signPayProductMapper.updateByPrimaryKeySelective(signPayProduct);
    }

    @Override
    public void updateApplyInfo(SignPayProductDTO dto) {
        SignPayProduct signPayProduct=signPayProductMapper.selectByPrimaryKey(dto.getId());

        MerchantTwo2DTO merchantTwo2DTO=new MerchantTwo2DTO();
        merchantTwo2DTO.setSignedName(dto.getMerchantTwoDTO().getSignedName());
        merchantTwo2DTO.setEmail(dto.getMerchantTwoDTO().getEmail());
        merchantTwo2DTO.setPhone(dto.getMerchantTwoDTO().getPhone());
        merchantTwo2DTO.setProfession(dto.getMerchantTwoDTO().getProfession());
        merchantTwo2DTO.setZzLegalPersonIdNo(dto.getMerchantTwoDTO().getZzLegalPersonIdNo());
        merchantTwo2DTO.setAccountName(dto.getMerchantTwoDTO().getAccountName());
        merchantTwo2DTO.setBankCardNo(dto.getMerchantTwoDTO().getBankCardNo());
        merchantTwo2DTO.setBusinessAddress(dto.getMerchantTwoDTO().getBusinessAddress());
        merchantTwo2DTO.setBusinessAddressArea(dto.getMerchantTwoDTO().getBusinessAddressArea());
        merchantTwo2DTO.setBusinessAddressCity(dto.getMerchantTwoDTO().getBusinessAddressCity());
        merchantTwo2DTO.setBusinessAddressProvince(dto.getMerchantTwoDTO().getBusinessAddressProvince());
        merchantTwo2DTO.setChannelMerchantId(dto.getMerchantTwoDTO().getChannelMerchantId());
        merchantTwo2DTO.setProvinceCode(dto.getMerchantTwoDTO().getProvinceCode());
        merchantTwo2DTO.setCityCode(dto.getMerchantTwoDTO().getCityCode());
        merchantTwo2DTO.setEmail(dto.getMerchantTwoDTO().getEmail());
        merchantTwo2DTO.setLegalIdCardConsPath(dto.getMerchantTwoDTO().getLegalIdCardConsPath());
        merchantTwo2DTO.setLegalPersonBankCardPath(dto.getMerchantTwoDTO().getLegalPersonBankCardPath());
        merchantTwo2DTO.setLegalIdCardProsPath(dto.getMerchantTwoDTO().getLegalIdCardProsPath());
        String applyInfo= JsonUtils.toJson(merchantTwo2DTO);
        signPayProduct.setApplyInfo(applyInfo);
        signPayProductMapper.updateByPrimaryKey(signPayProduct);
    }

    @Override
    public SignPayProductDTO getSignPayProductDTOById(Integer id) {
        SignPayProduct signPayProduct=signPayProductMapper.selectByPrimaryKey(id);
        SignPayProductDTO dto=new SignPayProductDTO();
        dto.setId(signPayProduct.getId());
        dto.setApplyStatus(signPayProduct.getApplyStatus());
        dto.setChannelMerchantId(signPayProduct.getChannelMerchantId());
        dto.setCreateTime(signPayProduct.getCreateTime());
        dto.setFailMsg(signPayProduct.getFailMsg());
        dto.setPayCode(signPayProduct.getPayCode());
        if (StrUtil.isNotEmpty(signPayProduct.getApplyInfo())) {
            if ("AYB_PAY".equals(signPayProduct.getPayCode())) {
                MerchantTwoDTO merchantTwoDTO = JsonUtils.fromJson(signPayProduct.getApplyInfo(), MerchantTwoDTO.class);
                merchantTwoDTO.setBankCardNo(SensitiveDataUtil.cardNoDesensitization(merchantTwoDTO.getBankCardNo()));
                merchantTwoDTO.setPhone(SensitiveDataUtil.mobileEncrypt(merchantTwoDTO.getPhone()));
                merchantTwoDTO.setZzLegalPersonIdNo(SensitiveDataUtil.cardNoDesensitization(merchantTwoDTO.getZzLegalPersonIdNo()));
                dto.setMerchantTwoDTO(merchantTwoDTO);
            } else if ("AYF_SPLIT_PAY".equals(signPayProduct.getPayCode())) {
                MerchantTwoAyfDTO dto1 = JsonUtils.fromJson(signPayProduct.getApplyInfo(), MerchantTwoAyfDTO.class);
                dto1.setBankCardNo(SensitiveDataUtil.cardNoDesensitization(dto1.getBankCardNo()));
                dto1.setPhone(SensitiveDataUtil.mobileEncrypt(dto1.getPhone()));
                dto1.setIdCardNo(SensitiveDataUtil.cardNoDesensitization(dto1.getIdCardNo()));
                dto.setMerchantTwoAyfDTO(dto1);
            }else if ("WXYFT".equals(signPayProduct.getPayCode())) {
                MerchantTwoAyfDTO dto1 = JsonUtils.fromJson(signPayProduct.getApplyInfo(), MerchantTwoAyfDTO.class);
                dto1.setBankCardNo(SensitiveDataUtil.cardNoDesensitization(dto1.getBankCardNo()));
                dto1.setPhone(SensitiveDataUtil.mobileEncrypt(dto1.getPhone()));
                dto1.setIdCardNo(SensitiveDataUtil.cardNoDesensitization(dto1.getIdCardNo()));
                dto.setMerchantTwoAyfDTO(dto1);
            }else if ("ZFYFZ".equals(signPayProduct.getPayCode())) {
                ZFYAccountCreateReq2 dto1 = JsonUtils.fromJson(signPayProduct.getApplyInfo(), ZFYAccountCreateReq2.class);
                dto.setZfyAccountCreateReq2(dto1);
            }
        }
        return dto;
    }

    @Override
    public SignPayProductDTO getSignPayProductDTOByMId(Integer mId, String payCode) {
        Integer num=paymentProductService.countProductAndMerchant(mId,payCode);
        SignPayProductDTO dto=new SignPayProductDTO();
        if(num==0){
            dto.setPayProductNum(0);
            return dto;
        }
        dto.setPayProductNum(1);
        Example example = new Example(SignPayProduct.class);
        example.createCriteria()
                .andEqualTo("merchantId", mId)
                .andEqualTo(SignPayProduct.Fields.payCode, payCode);
        SignPayProduct signPayProduct= signPayProductMapper.selectOneByExample(example);
        if(signPayProduct==null){
            dto.setCountSignPayProduct(0);
            return dto;
        }
        dto.setCountSignPayProduct(1);
        dto.setUpdateStatus(signPayProduct.getUpdateStatus());
        dto.setId(signPayProduct.getId());
        dto.setApplyStatus(signPayProduct.getApplyStatus());
        dto.setMerchantId(signPayProduct.getMerchantId());
        dto.setChannelMerchantId(signPayProduct.getChannelMerchantId());
        dto.setCreateTime(signPayProduct.getCreateTime());
        dto.setFailMsg(signPayProduct.getFailMsg());
        dto.setPayCode(signPayProduct.getPayCode());
        MerchantTwoDTO merchantTwoDTO=JsonUtils.fromJson(signPayProduct.getApplyInfo(),MerchantTwoDTO.class);
        dto.setMerchantTwoDTO(merchantTwoDTO);
        return dto;
    }

    /**
     * 根据商户id和payCode获取
     * @author lch
     * @date 2022/1/19
     */
    @Override
    public SignPayProductDTO getByMerchantIdAndPayCode(Integer merchantId, String payCode) {

        SignPayProductDTO dto=new SignPayProductDTO();
        Example example = new Example(SignPayProduct.class);
        example.createCriteria()
                .andEqualTo(SignPayProduct.Fields.merchantId, merchantId)
                .andEqualTo(SignPayProduct.Fields.payCode, payCode);
//        SignPayProduct signPayProduct= signPayProductMapper.selectOneByExample(example);
        List<SignPayProduct> list = signPayProductMapper.selectByExample(example);
        if(CollectionUtils.isEmpty(list)){
            return dto;
        }
        LogPortal.info("List<SignPayProduct> list = {}", JSONObject.toJSONString(list));
        SignPayProduct signPayProduct = list.get(0);
//        if(signPayProduct==null){
//            return dto;
//        }
        dto = BeanConvertUtils.convert(signPayProduct, SignPayProductDTO.class);
        dto.setCountSignPayProduct(1);
        MerchantTwoDTO merchantTwoDTO=JsonUtils.fromJson(signPayProduct.getApplyInfo(),MerchantTwoDTO.class);
        dto.setMerchantTwoDTO(merchantTwoDTO);
        return dto;
    }

    @Override
    public SignPayProductDTO getByIdCardAndPayCode(String idCard, String payCode) {
        return signPayProductMapper.getByIdCardAndPayCode(idCard,payCode);
    }

    @Override
    public List<SignPayProductDTO> getSignPayProductList(SignPayProductDTO dto) {
        return signPayProductMapper.getSignPayProductList(dto);
    }

    @Override
    public List<SignPayProductDTO2> selectPageSignPayProduct(SignPayProductCriteria selectCriteria) {
        return signPayProductMapper.selectPageSignPayProduct(selectCriteria);
    }

    /**
     * 删除
     * @author lch
     * @date 2022/1/20
     */
	@Override
	public void removeById(Integer id) {
		this.signPayProductMapper.deleteByPrimaryKey(id);
	}

    @Override
    public List<SignPayProductDTO> litReviewSignPayProduct() {
        return signPayProductMapper.litReviewSignPayProduct();
    }

    @Override
    public void deleteByMerchantIdAndCode(Integer merchantId, String payCode) {
        signPayProductMapper.deleteByMerchantIdAndCode(merchantId, payCode);
    }
}
